﻿Imports System.Net

Public Class HttpClient
    Inherits WebClient
    Private cookieCon As CookieContainer

    Private _timeOut As Integer = 10
    Private _method As String = "GET"

    '

    ''' <summary>
    ''' 过期时间
    ''' </summary>
    Public Property Timeout() As Integer
        Get
            Return _timeOut
        End Get
        Set(value As Integer)
            If value <= 0 Then
                _timeOut = 10
            End If
            _timeOut = value
        End Set
    End Property

    ''' <summary>
    ''' 方法
    ''' </summary>
    Public Property Method() As String
        Get
            Return _method
        End Get
        Set(value As String)
            Me._method = value
        End Set
    End Property

    '

    ''' <summary>
    ''' 重写GetWebRequest,添加WebRequest对象超时时间（秒）
    ''' </summary>
    ''' <param name="address"></param>
    ''' <returns></returns>
    Protected Overrides Function GetWebRequest(address As Uri) As WebRequest
        Dim request As HttpWebRequest = MyBase.GetWebRequest(address)
        request.Timeout = 1000 * Timeout
        request.ReadWriteTimeout = 1000 * Timeout
        'request.Method = this._method;
        request.KeepAlive = False
        request.ServicePoint.Expect100Continue = False
        If TypeOf request Is HttpWebRequest Then
            Dim httpRequest As HttpWebRequest = TryCast(request, HttpWebRequest)
            httpRequest.CookieContainer = cookieCon
            Return httpRequest
        End If
        Return request
    End Function

    ''' <summary>
    ''' 创建一个新实例
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        cookieCon = New CookieContainer
    End Sub

    ''' <summary>
    ''' 创建一个新的实例带cookies
    ''' </summary>
    ''' <param name="cookies"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal cookies As CookieContainer)
        cookieCon = cookies
    End Sub

    Public Property Cookies() As CookieContainer
        Get
            Return cookieCon
        End Get
        Set(ByVal value As CookieContainer)
            cookieCon = value
        End Set
    End Property

    ''' <summary>
    ''' POST提交数据
    ''' </summary>
    ''' <param name="url">网址</param>
    ''' <param name="data">数据</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function PostData(ByVal url As String, ByVal data As String)
        Return PostData(url, data, Text.Encoding.UTF8)
    End Function

    ''' <summary>
    ''' POST提交数据
    ''' </summary>
    ''' <param name="url">网址</param>
    ''' <param name="data">数据</param>
    ''' <param name="dataEncoding">编码</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function PostData(ByVal url As String, ByVal data As String, ByVal dataEncoding As Text.Encoding)
        Try
            Dim _postdata As Byte() = dataEncoding.GetBytes(data)
            Headers.Add("Content-Type", "application/x-www-form-urlencoded")
            Dim _responsedata As Byte() = UploadData(url, "POST", _postdata)
            Dim retVal As String = dataEncoding.GetString(_responsedata)
            Return retVal
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

End Class
